package com.rae.VoiceRecognition;

import java.util.ArrayList;
import java.util.List;
 
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class VoiceRecognition extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	private static final int REQUEST_CODE = 1234;
    private ListView mList;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button speakButton = (Button) findViewById(R.id.speak_btn);
        speakButton.setText("voice recognition");
         
        mList = (ListView) findViewById(R.id.list);
        
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_WEB_SEARCH), 0);
        
        if (activities.size() != 0){
                speakButton.setOnClickListener(this);
        } else {
                speakButton.setEnabled(false);
                speakButton.setText("Recognizer not Present");
                addAccount();
                getProfileProxy();
        }
    }
 
    private void startVoiceRecognitionActivity() {
        // TODO Auto-generated method stub
        try{
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Free Form Language Model Demo");
            startActivityForResult(intent, REQUEST_CODE);
        } catch (ActivityNotFoundException ex) {
            Toast.makeText(VoiceRecognition.this, "Activity Not Found", Toast.LENGTH_LONG).show();
        }
    }
 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
         
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK){
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, matches));
        }
         
        super.onActivityResult(requestCode, resultCode, data);
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		 if (v.getId() == R.id.speak_btn){
	            startVoiceRecognitionActivity();
	        }       
	}
}